home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 742 / rkrm_lib2 / rkrm_lib2.lha / Exec_Library / Memory / allocentry.c < prev   
C/C++ Source or Header  |  1992-09-03  |  4KB  |  82 lines

  1. ;/* allocentry.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 allocentry.c
  3. Blink FROM LIB:c.o,allocentry.o TO allocentry LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit ;
  5.  
  6. allocentry.c - example of allocating several memory areas.
  7.  
  8.  
  9. Copyright (c) 1992 Commodore-Amiga, Inc.
  10.  
  11. This example is provided in electronic form by Commodore-Amiga, Inc. for
  12. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  13. published by Addison-Wesley (ISBN 0-201-56774-1).
  14.  
  15. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  16. information on the correct usage of the techniques and operating system
  17. functions presented in these examples.  The source and executable code
  18. of these examples may only be distributed in free electronic form, via
  19. bulletin board or as part of a fully non-commercial and freely
  20. redistributable diskette.  Both the source and executable code (including
  21. comments) must be included, without modification, in any copy.  This
  22. example may not be published in printed form or distributed with any
  23. commercial product.  However, the programming techniques and support
  24. routines set forth in these examples may be used in the development
  25. of original executable software products for Commodore Amiga computers.
  26.  
  27. All other rights reserved.
  28.  
  29. This example is provided "as-is" and is subject to change; no
  30. warranties are made.  All use is at your own risk. No liability or
  31. responsibility is assumed.
  32. */
  33.  
  34. #include <exec/types.h>
  35. #include <exec/memory.h>
  36. #include <clib/exec_protos.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39.  
  40. #ifdef LATTICE
  41. int CXBRK(void)  { return(0); }  /* Disable Lattice CTRL/C handling */
  42. void chkabort(void) { return; }  /* really */
  43. #endif
  44.  
  45. #define ALLOCERROR 0x80000000
  46.  
  47. struct MemList *memlist;             /* pointer to a MemList structure        */
  48.  
  49. struct MemBlocks /* define a new structure because C cannot initialize unions */
  50. {
  51.     struct MemList  mn_head;         /* one entry in the header               */
  52.     struct MemEntry mn_body[3];      /* additional entries follow directly as */
  53. } memblocks;                         /* part of the same data structure       */
  54.  
  55. VOID main(VOID)
  56. {
  57.     memblocks.mn_head.ml_NumEntries = 4; /* 4! Since the MemEntry starts at 1! */
  58.  
  59.     /* Describe the first piece of memory we want.  Because of our MemBlocks structure */
  60.     /* setup, we reference the first MemEntry differently when initializing it.        */
  61.     memblocks.mn_head.ml_ME[0].me_Reqs = MEMF_CLEAR;
  62.     memblocks.mn_head.ml_ME[0].me_Length = 4000;
  63.  
  64.     memblocks.mn_body[0].me_Reqs   = MEMF_CHIP | MEMF_CLEAR;   /* Describe the other pieces of    */
  65.     memblocks.mn_body[0].me_Length = 100000;                   /* memory we want. Additional      */
  66.     memblocks.mn_body[1].me_Reqs   = MEMF_PUBLIC | MEMF_CLEAR; /* MemEntries are initialized this */
  67.     memblocks.mn_body[1].me_Length = 200000;                   /* way. If we wanted even more en- */
  68.     memblocks.mn_body[2].me_Reqs   = MEMF_PUBLIC;              /* tries, we would need to declare */
  69.     memblocks.mn_body[2].me_Length = 25000;                    /* a larger MemEntry array in our  */
  70.                                                                /* MemBlocks structure.            */
  71.  
  72.     memlist = (struct MemList *)AllocEntry((struct MemList *)&memblocks);
  73.  
  74.     if ((ULONG)memlist & ALLOCERROR)          /* 'error' bit 31 is set (see below). */
  75.     {
  76.        printf("AllocEntry FAILED\n");
  77.        exit(200);
  78.     }
  79.     /* We got all memory we wanted.  Use it and call FreeEntry() to free it */
  80.     printf("AllocEntry succeeded - now freeing all allocated blocks\n");
  81.     FreeEntry(memlist);
  82. }